﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Input;
using Dominion.Core;
using Dominion.WPF.Commands;
using Dominion.WPF.Services;

namespace Dominion.WPF.ViewModels
{
    public class PlayerViewModel : ViewModelBase
    {

        public PlayerViewModel()
            : base()
        {


        }


        #region CollectionViews
        private ICollectionView deckView;
        public ICollectionView DeckView
        {
            get
            {
                if (UserContext.CurrentGame == null) return null;
                if (deckView == null)
                    deckView = CollectionViewSource.GetDefaultView(UserContext.CurrentPlayer.Cards.Deck);

                return deckView;
            }
        }


        private ICollectionView handView;
        public ICollectionView HandView
        {
            get
            {

               
                if (handView == null)
                {
                    handView = CollectionViewSource.GetDefaultView(UserContext.CurrentPlayer.Cards.Hand);
                    handView.SortDescriptions.Clear();
                    handView.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
                }
                return handView;
            }
        }

        private ICollectionView discardView;
        public ICollectionView DiscardView
        {
            get
            {
               
                if (discardView == null)
                    discardView = CollectionViewSource.GetDefaultView(UserContext.CurrentPlayer.Cards.Discard);

                return discardView;
            }
        }


        private ICollectionView playedCardsView;
        public ICollectionView PlayedCardsView
        {
            get
            {
               
                if (playedCardsView == null)
                    playedCardsView = CollectionViewSource.GetDefaultView(UserContext.CurrentPlayer.Cards.Played);
                return playedCardsView;
            }
        }


        private ICollectionView buyedCardsView;
        public ICollectionView BuyedCardsView
        {
            get
            {
               
                if (buyedCardsView == null)
                    buyedCardsView = CollectionViewSource.GetDefaultView(UserContext.CurrentPlayer.Cards.Buyed);
                return buyedCardsView;
            }
        }


        #endregion

        public Player CurrentPlayer
        {
            get { return UserContext.CurrentPlayer; }
        }


        public Card SelectedCardInHand { get; set; }

        private RelayCommand playActionCommand;
        public ICommand PlayActionCommand
        {
            get
            {
                if (playActionCommand == null)
                    playActionCommand = new RelayCommand(
                        (x) => PlayCard(SelectedCardInHand as Kingdom ),
                        (x) => (SelectedCardInHand is Kingdom) && ( UserContext.CurrentPlayer.ActionCount > 0)
                        );
                return playActionCommand;
            }
        }


        private void PlayCard(Kingdom card)
        {
            UserContext.CurrentPlayer.Play(card);
        }



    }
}
